var QTYYSysFunctionMain = function() {
	// 主页面
	var $refreshTreeBtn = $("#refreshTreeBtn");
	var sysFunctionTreeId = "sysFunctionTree";
	var treeAjaxUrl = "sysFunction/list";
	var sysFunctionInfoFormId = "sysFunctionInfoForm";
	var $sysFunctionInfoForm = $("#sysFunctionInfoForm");

	var setting = {
		async : {
			enable : true,
			url : "/sysFunction/list",
			type : "get",
			dataFilter : function(treeId, parentNode, responseData) {
				if (responseData.success == true) {
					responseData = responseData.obj;
					return responseData;
				} else {
					QTYYComponents.bootstrapSweetAlert("", responseData.msg, "error");
				}
			}
		},
		view : {
			showLine : true,
			addHoverDom : function(treeId, treeNode) {
				var sObj = $("#" + treeNode.tId + "_span");
				if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) {
					return;
				}
				var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
						+ "' title='add node' onfocus='this.blur();'></span>";
				sObj.after(addStr);
				var btn = $("#addBtn_" + treeNode.tId);
				if (btn) {
					btn.bind("click", function() {
						QTYYComponents.layerOpen("增加菜单", '800px', '350px', "sysFunction/add/view?funId=" + treeNode.id)
					});
				}
			},
			removeHoverDom : function(treeId, treeNode) {
				$("#addBtn_" + treeNode.tId).unbind().remove();
			},
			selectedMulti : false
		},
		data : {
			simpleData : {
				enable : true,
				idKey : "id",
				pidKey : "pId",
				rootPid : 0
			}
		},
		check : {
			enable : false,// 在节点前显示checkbox
			chkboxType : {// 节点级联
				"Y" : "",
				"N" : ""
			}
		},
		edit : {
			enable : true
		},
		callback : {
			onClick : function(event, treeId, treeNode) {
				showSysFunctionInfo(treeNode);
			},
			beforeEditName : function(treeId, treeNode) {
				QTYYComponents.layerOpen("修改资源", '800px', '350px', "sysFunction/update/view?funId=" + treeNode.id);
			},
			beforeRemove : function(treeId, treeNode) {
				QTYYComponentsTree.ajaxDeleteTree("/sysFunction/delete", treeId, treeNode);
				return false;
			}
		}
	};

	var sysFunctionMainEventHandler = function() {
		$refreshTreeBtn.on("click", function() {
			QTYYComponentsTree.refreshTree(sysFunctionTreeId);
		});
	}

	// 初始化树
	var initSysFunctionTree = function(sysFunctionTreeId, setting) {
		QTYYComponentsTree.initTree(sysFunctionTreeId, setting);
	}

	// 展示选中的菜单信息
	var showSysFunctionInfo = function(treeNode) {
		$.ajax({
			type : "GET",
			cache : false,
			url : "sysFunction/info" + "/" + treeNode.id,
			success : function(responseJson) {
				if (responseJson.success == true) {// 返回true
					$("#sysFunctionName").html("菜单名称:" + treeNode.name);
					var obj = responseJson.obj;
					if (null != obj && "" != obj) {
						$.each($sysFunctionInfoForm.find("input"), function(index, value) {
							var element = $(value);
							var key = element.attr("name");
							if (typeof (key) != "undefined") {
								if (key == "funType") {// 数字变成文字
									if (obj[key] == 1) {// 菜单
										element.val("菜单");
									} else if (obj[key] == 0) {// 按钮
										element.val("按钮");
									}
								} else {
									element.val(obj[key]);
								}
							}
						});
					} else {
						QTYYComponents.bootstrapSweetAlert("", "获取数据为空值", "error");
					}
				} else {
					QTYYComponents.bootstrapSweetAlert("", responseJson.msg, "error");
				}
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
				QTYYComponents.bootstrapSweetAlert("", "系统错误!!!", "error");
			}
		});
	}

	return {
		// 初始主界面树
		initSysFunctionMain : function() {
			initSysFunctionTree(sysFunctionTreeId, setting);
			sysFunctionMainEventHandler();
		}
	}

}();
